#include "Brick.h"

CBrick::CBrick(CPhysics* pPhysics)
{
	//non-ODE init routnes
	m_iRemoveTimer = -1;

	//ODE init routines
	m_body = dBodyCreate( pPhysics->GetWorld() );
		

	m_geom = dCreateBox(pPhysics->GetSpace(), 0.9, 0.9, 0.9);
		
	dGeomSetBody(m_geom, m_body);

	dMassSetBox(&m_mass, 1, 0.9f, 0.9f, 0.9f);
	dBodySetMass(m_body, &m_mass);

	dBodySetPosition(m_body, 0.0f, 0.0f, 0.0f);

	dBodySetData(m_body, this);

	m_iValue = 25;
		
//	dRSetIdentity (R);
//	dBodySetRotation(body, R);
}

CBrick::~CBrick(void)
{
	dBodyDestroy(m_body);
	dGeomDestroy(m_geom);
}

int CBrick::Draw()
{
	glPushMatrix();

	SetupMatrices();

	glEnable(GL_TEXTURE_2D);
	glBindTexture( GL_TEXTURE_2D, m_uiTexture );

	GLfloat WhiteSurface[] = { 1.0f, 1.0f, 1.0f, 1.0f};
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, WhiteSurface);
	glBegin(GL_QUADS);
	/* przednia sciana */
	glNormal3d( 0.0, 0.0, -1.0);
//	glColor3d(0.0, 1.0,0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, -0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, -0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, -0.45, -0.45);

	/* tylna sciana */
	glNormal3d( 0.0, 0.0, 1.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, 0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, -0.45, 0.45);

	/* gorna sciana */
	glNormal3d( 0.0, 1.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, 0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, 0.45, -0.45);

	/* dolna sciana */
	glNormal3d( 0.0, -1.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, -0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, -0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, -0.45, -0.45);
	
	/* lewa boczna sciana */
	glNormal3d( -0.0, 0.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, -0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( -0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( -0.45, -0.45, 0.45);

	/* prawa boczna sciana */
	glNormal3d( 0.0, 0.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( 0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( 0.45, -0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, 0.45, -0.45);

	glEnd();

	glDisable(GL_TEXTURE_2D);

	glPopMatrix();
	return 0;
}

int CBrick::Update()
{
	if(m_iRemoveTimer==0)
	{
		m_pBat->Score(m_iValue);
		Remove();
	}
	else
	{
		m_iRemoveTimer--;
	}
	return 0;
}

int CBrick::SetPosition(double dX, double dY, double dZ)
{
	dBodySetPosition(m_body, dX, dY, dZ);
	return 0;
}

int CBrick::Hit(CBall* pBall)
{
	m_iRemoveTimer = 20;
	m_pBat = pBall->GetRecent();
	return 0;
}

void CBrick::LoadTexture()
{
	m_uiTexture = CUtils::LoadRAW("brick.raw", 256, 256);
}